/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.lzh.compiler.parcelerdemo;

import com.lzh.compiler.parceler.BundleFactory;
import com.lzh.compiler.parceler.Parceler;
import com.lzh.compiler.parceler.annotation.BundleBuilder;
import com.lzh.compiler.parcelerdemo.bean.Book;
import com.lzh.compiler.parcelerdemo.bean.Info;
import com.lzh.compiler.parcelerdemo.bean.NormalUser;
import com.lzh.compiler.parcelerdemo.bean.SerialUser;
import com.lzh.compiler.parcelerdemo.utils.Const;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.agp.components.Button;
import ohos.agp.components.Text;

import java.lang.reflect.Type;
import java.util.Set;

/**
 * BundleAbility.
 */
@BundleBuilder
public class BundleAbility extends Ability {
    private BundleFactory factory;
    private Text result;
    private Button putNormalData;
    private Button putSpecialData;
    private Button getDataWithoutParceler;
    private Button getDataWithParceler;
    private Button testSerialAndNormalStore;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_bundle_ablity);

        factory = Parceler.createFactory(new IntentParams());
        if (findComponentById(ResourceTable.Id_result) instanceof Text) {
            result = (Text) findComponentById(ResourceTable.Id_result);
        }
        if (findComponentById(ResourceTable.Id_put_normal_data) instanceof Button) {
            putNormalData = (Button) findComponentById(ResourceTable.Id_put_normal_data);
        }
        if (findComponentById(ResourceTable.Id_put_special_data) instanceof Button) {
            putSpecialData = (Button) findComponentById(ResourceTable.Id_put_special_data);
        }
        if (findComponentById(ResourceTable.Id_get_data_without_parceler) instanceof Button) {
            getDataWithoutParceler = (Button) findComponentById(ResourceTable.Id_get_data_without_parceler);
        }
        if (findComponentById(ResourceTable.Id_get_data_with_parceler) instanceof Button) {
            getDataWithParceler = (Button) findComponentById(ResourceTable.Id_get_data_with_parceler);
        }
        if (findComponentById(ResourceTable.Id_test_serial_normal_store) instanceof Button) {
            testSerialAndNormalStore = (Button) findComponentById(ResourceTable.Id_test_serial_normal_store);
        }
        getDataClickEvent();
    }

    private void getDataClickEvent() {
        putNormalData.setClickedListener(component -> {
            factory.put("int", 1);
            factory.put("string", "Hello world");
            factory.put("parcelable", new Info());
            result.setText("存入普通数据成功");
        });
        putSpecialData.setClickedListener(component -> {
            factory.put("book", new Book("逗逼是怎么炼成的", 66.66f));
            result.setText("存入特殊数据成功");
        });
        getDataWithoutParceler.setClickedListener(component -> {
            IntentParams bundle = factory.getBundle();
            Set<String> keys = bundle.keySet();

            StringBuilder builder = new StringBuilder("不使用Parceler进行Bundle数据读取展示：");
            for (String key : keys) {
                builder.append(Const.SLACE_R_N);
                builder.append(key + Const.EQUAL + bundle.getParam(key));
            }
            result.setText(builder.toString());
        });
        getDataWithParceler.setClickedListener(component -> {
            StringBuilder builder = new StringBuilder("使用Parceler进行Bundle数据读取展示：");
            resolveKeyValue(builder, "int", int.class);
            resolveKeyValue(builder, "string", String.class);
            resolveKeyValue(builder, "parcelable", Info.class);
            resolveKeyValue(builder, "book", Book.class);
            result.setText(builder.toString());
        });
        testSerialAndNormalStore.setClickedListener(component -> {
            NormalUser normalUser = new NormalUser();
            SerialUser serialUser = new SerialUser();
            normalUser.username = "this is normal user bean";
            serialUser.username = "this is serial user bean";
            BundleFactory factory = Parceler.createFactory(new IntentParams());
            factory.put("normal", normalUser);
            factory.put("serial", serialUser);
            IntentParams result1 = factory.getBundle();
            NormalUser newNormalUser = factory.get("normal", NormalUser.class);
            SerialUser newSerialUser = factory.get("serial", SerialUser.class);
        });
    }

    private void resolveKeyValue(StringBuilder builder, String key, Type type) {
        builder.append(Const.SLACE_R_N);
        builder.append(key + " = " + factory.get(key, type));
    }
}

